package com.catmiw.stock.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.catmiw.stock.service.SessionFactory;
import com.catmiw.stock.dao.StockMapper;
import com.catmiw.stock.domain.Stockdb;

public class StockServiceImpl implements StockService {
	public int insertAll(List<Stockdb> stocks) {
		SqlSession session=SessionFactory.getInstance().getSession();
		
		StockMapper cm=session.getMapper(StockMapper.class);
		List<Stockdb> old=cm.selectAll();
		int count=0;
		for (Stockdb c:stocks){
			if (old!=null&&old.size()>0&&old.contains(c)){
				cm.update(c);
			} else {
				cm.insert(c);
				count++;
			}
		}
		session.commit();
		session.close();
		
		return count;
	}

	public void deleteAll() {
		SqlSession session=SessionFactory.getInstance().getSession();
		
		StockMapper cm=session.getMapper(StockMapper.class);
		cm.deleteAll();
		session.commit();
		session.close();
	}

}
